<!DOCTYPE html>
<html>
<head>
    <title>DeepSeek PHP Clone</title>
    <!-- 在 head 中添加 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/styles/default.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/highlight.min.js"></script>
    <style>
        /* 精简版样式（完整样式可参考之前回答） */
        body { margin: 0; font-family: Arial; }
        .container { max-width: 800px; margin: 20px auto; }
        #chatBox { height: 500px; border: 1px solid #ccc; overflow-y: auto; padding: 10px; }
        .user-msg { background: #e3f2fd; margin: 5px; padding: 10px; border-radius: 5px; }
        .bot-msg { background: #f5f5f5; margin: 5px; padding: 10px; border-radius: 5px; }
        #inputBox { width: 100%; padding: 10px; margin-top: 10px; }
        button { padding: 10px 20px; background: #2196F3; color: white; border: none; cursor: pointer; }
    </style>
</head>
<body>
    <div class="container">
        <div id="chatBox"></div>
        <textarea id="inputBox" placeholder="输入你的问题..." rows="4"></textarea>
        <button onclick="sendMessage()">发送</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>

    <script>
        let conversationHistory = [];

        async function sendMessage() {
            const input = document.getElementById('inputBox');
            const message = input.value.trim();
            if (!message) return;

            // 添加用户消息到界面
            addMessage('user', message);
            input.value = '';

            try {
                const response = await fetch('api.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        message: message,
                        history: conversationHistory
                    })
                });

                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let botResponse = '';
                const botMsgDiv = addMessage('bot', ''); // 创建空消息容器

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const textChunk = decoder.decode(value);
                    botResponse += textChunk;
                    botMsgDiv.innerHTML = botResponse; // 实时更新内容
                    botMsgDiv.scrollIntoView();
                }

                // 保存对话历史
                conversationHistory.push({ role: 'user', content: message });
                conversationHistory.push({ role: 'assistant', content: botResponse });

            } catch (error) {
                console.error('Error:', error);
                addMessage('bot', '⚠️ 请求失败，请检查控制台');
            }
        }

        function addMessage(role, text) {
            const chatBox = document.getElementById('chatBox');
            const div = document.createElement('div');
            div.className = role + '-msg';
            
            //div.innerHTML = text;
            // 修改 addMessage 函数
            div.innerHTML = marked.parse(text);

            // ...原有代码...
            hljs.highlightAll();

            chatBox.appendChild(div);
            return div;
        }

        // 支持回车键发送（Ctrl+Enter换行）
        document.getElementById('inputBox').addEventListener('keydown', (e) => {
            if (e.key === 'Enter' && !e.ctrlKey) {
                e.preventDefault();
                sendMessage();
            }
        });
    </script>
</body>
</html>